package base.utils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 说明：
 * Created by jjs on 2019/1/23
 */
public abstract class DownLoadObserver implements Observer<DownloadInfo> {
    protected Disposable d;//可以用于取消注册的监听者
    protected DownloadInfo downloadInfo;

    @Override
    public void onSubscribe(Disposable d) {
        this.d = d;
    }

    @Override
    public void onNext(DownloadInfo downloadInfo) {
        if (this.downloadInfo == null) {
            this.downloadInfo = new DownloadInfo(downloadInfo.getUrl());
            this.downloadInfo.setFileName(downloadInfo.getFileName());
            this.downloadInfo.setTotal(downloadInfo.getTotal());
        }
        if (this.downloadInfo.getTotal() != downloadInfo.getTotal() || this.downloadInfo.getProgress() != downloadInfo.getProgress()) {
            this.downloadInfo.setProgress(downloadInfo.getProgress());
            _onNext(this.downloadInfo);
        }
    }


    public abstract void _onNext(DownloadInfo downloadInfo);
}
